//
//  FSCExaminationController.swift
//  FamiliesSchoolConnection
//
//  Created by 花花蔡 on 2016/12/22.
//  Copyright © 2016年 com.HuaHuaCai. All rights reserved.
//

import UIKit

class FSCExaminationController: UITableViewController {

    fileprivate static let examCell = "examCell"
    fileprivate let examinationVM  = FSCExaminationListViewModel()
    fileprivate var cellRow:Int = -1
    
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.backgroundColor = UIColor.colorWithHex(hexColor: grayColor)
        tableView.rowHeight = 140 * KWidth_ScaleW
        tableView.register(UINib.init(nibName: "FSCExamCell", bundle: nil), forCellReuseIdentifier:FSCExaminationController.examCell)
        downloadData()
   }

    private func downloadData() -> Void {
        examinationVM.loadData { [weak self](isok) in
            if isok {
                self?.tableView.reloadData()
            }
        }
    }
}


// MARK: - Table view data source
extension FSCExaminationController {
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return examinationVM.examinationModelArr.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: FSCExaminationController.examCell, for: indexPath) as! FSCExamCell
    
        let model = examinationVM.examinationModelArr[indexPath.row]
        cell.model = model
        return cell
    }

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let cell = tableView.cellForRow(at: indexPath) as! FSCExamCell
        if cell.bgView.isHidden == true {
            cell.bgView.isHidden = false
            cellRow = indexPath.row
        }else {
            cell.bgView.isHidden = true
            cellRow = -1

        }
        cell.selectionStyle = .none
        tableView.reloadData()
    }
    
    override func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.row == cellRow {
            tableView.rowHeight = 140 * KWidth_ScaleW
            return tableView.rowHeight
        }else {
            return 100*KWidth_ScaleW
        }
        
    }
}
